package com.aqie.rabbit.service.Impl;

import com.aqie.rabbit.bean.api.message.MessageCreateModel;
import com.aqie.rabbit.bean.db.Group;
import com.aqie.rabbit.bean.db.Message;
import com.aqie.rabbit.bean.db.User;
import com.aqie.rabbit.repositories.MessageRepository;
import com.aqie.rabbit.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Override
    public Message findById(String id) {
        return messageRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional
    public Message add(User sender, User receiver, MessageCreateModel model) {
        Message message = new Message(sender,receiver,model);
        return messageRepository.save(message);
    }

    @Override
    @Transactional
    public Message add(User sender, Group group, MessageCreateModel model) {
        Message message = new Message(sender, group, model);
        return messageRepository.save(message);
    }
}
